import Vue from 'vue'
import Vuex from 'vuex'
import commonModules from '@/common/store'

import creditAgencyModule from '@/gov/store/modules/creditAgency'
import govAgencyModule from '@/credit/store/modules/govAgency'
import productCategory from '@/credit/store/modules/productCategory'
import industrialClass from '@/credit/store/modules/industrialClass'
import creditGoods from '@/gov/store/modules/creditGoods'

Vue.use(Vuex)

const files = require.context('./modules/', true, /\.js$/)
const appModules = files.keys().reduce((mod, path) => {
  const name = path.replace(/\.\/(\S+)\.js$/, '$1')
  mod[name] = files(path).default
  return mod
}, {})

const bankExclude = ['router']
const bankFiles = require.context('../../bank/store/modules/', true, /\.js$/)
const bankModules = bankFiles.keys().reduce((mod, path) => {
  const name = path.replace(/\.\/(\S+)\.js$/, '$1')
  if (bankExclude.includes(name)) return mod
  mod[name] = bankFiles(path).default
  return mod
}, {})

export default new Vuex.Store({
  modules: {
    ...appModules,
    ...commonModules,
    ...bankModules,
    creditAgencyModule,
    govAgencyModule,
    productCategory,
    industrialClass,
    creditGoods,
  },
  strict: process.env.NODE_ENV !== 'production',
})
